package yoyo.secret.app;

import java.util.List;

import yoyo.secret.app.base.YoyoApplication;
import yoyo.secret.app.db.DataBaseOperation;
import yoyo.secret.app.model.ClassIfication;
import yoyo.secret.app.model.ClassificationAdapter;
import yoyo.secret.app.widget.CustomDialog;
import yoyo.secret.app.widget.ToggleImageButton;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class YoyoActivity extends Activity {

	private CustomDialog selectDialog = null;
	private FrameLayout mainContetlayout;
	private LinearLayout linearLayout;
	private ClassificationAdapter adapter;

	private DataBaseOperation mDataBaseOperation;
	private ToggleImageButton addButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mainContetlayout = (FrameLayout) findViewById(R.id.content);
		mDataBaseOperation = ((YoyoApplication) getApplication()).dataBaseOperation;
		addButton = (ToggleImageButton) findViewById(R.id.add);
		addButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// showAddDialog();
				showOrHideInputBoxlayout(addButton.getButtonState());
			}
		});

		showClassification();
		initInputBox();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		adapter.notifyDataSetChanged();
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 添加类别界面
	 */
	private void initInputBox() {
		linearLayout = (LinearLayout) findViewById(R.id.add_keyword_content_layout_id);
		ImageButton cancelInputBoxBtn = (ImageButton) findViewById(R.id.cancel_btn_id);
		ImageButton submitInputBoxBtn = (ImageButton) findViewById(R.id.submit_btn_id);
		final EditText editText = (EditText) findViewById(R.id.keyword_text_id);

		cancelInputBoxBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// showOrHideInputBoxlayout(false);
				editText.setText(null);
			}
		});

		submitInputBoxBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String textInfoString = editText.getText().toString();
				// Log.i("", "------------------------>" + textInfoString);
				if (textInfoString.isEmpty()) {
					Toast.makeText(YoyoActivity.this, R.string.info_is_null_tips, Toast.LENGTH_SHORT).show();
					return;
				}

				long count = mDataBaseOperation.addClassification(textInfoString);
				// Log.i("", "-----------1------------->" + count);
				if (count > 0 && adapter != null) {
					ClassIfication classIfication = new ClassIfication();
					classIfication.className = textInfoString;
					adapter.addData(classIfication);
					editText.setText(null);
					showOrHideInputBoxlayout(false);
					addButton.setState(false);
				}
			}
		});
	}

	private void showOrHideInputBoxlayout(boolean param) {

		if (param) {
			linearLayout.setVisibility(View.VISIBLE);
		} else {
			linearLayout.setVisibility(View.GONE);
		}

	}

	private void showAddDialog() {
		selectDialog = new CustomDialog(YoyoActivity.this);
		selectDialog.show();

		selectDialog.setCancelOnclick(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				selectDialog = null;
			}
		});

		selectDialog.setOkOnclick(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String textInfoString = selectDialog.getContent();
				// Log.i("", "------------------------>" + textInfoString);
				if (textInfoString.isEmpty()) {
					Toast.makeText(YoyoActivity.this, R.string.info_is_null_tips, Toast.LENGTH_SHORT).show();
					return;
				}

				long count = mDataBaseOperation.addClassification(selectDialog.getContent());
				// Log.i("", "-----------1------------->" + count);
				if (count > 0 && adapter != null) {
					ClassIfication classIfication = new ClassIfication();
					classIfication.className = textInfoString;
					adapter.addData(classIfication);
				}
			}
		});
	}

	/**
	 * 显示账号类别
	 */
	private void showClassification() {
		mainContetlayout.removeAllViews();
		final List<ClassIfication> ifications = mDataBaseOperation.getClassificationList();
		ListView listView = (ListView) getLayoutInflater().inflate(R.layout.list_view_layout, null);
		mainContetlayout.addView(listView);
		adapter = new ClassificationAdapter(ifications, this, mDataBaseOperation);
		listView.setAdapter(adapter);
		// 点击item查看具体有哪些账号
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// Log.i("", "-----------position------------->" +
				// ifications.get(position).className);
				Intent intent = new Intent(YoyoActivity.this, ContentActivity.class);
				intent.putExtra("class", ifications.get(position).className);
				startActivityForResult(intent, 1000);
				overridePendingTransition(R.anim.bottom_to_top, R.anim.up_to_bottom);
			}
		});

	}
}